package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * A class to fix the date format that mobile IOs returns. The 
 * primary task is to put NSDate into a format that the readable 
 * to the user with am and pm and no extra zeros.
 *  
 */

import java.util.HashMap;

import org.xmlvm.iphone.NSDate;

public class dateParser {
	public HashMap<String, String> monthName = new HashMap<String, String>();
	public String fullDate;
	public String year;
	public String month;
	public String day;
	public String hour;
	public String minute;
	public String amPm;
	
	public dateParser(NSDate inputDate){
		fullDate = inputDate.toString();
		initMonthMap();
		year = fullDate.substring(1, 4);
		month = monthName.get(fullDate.substring(5,7));
		day = Integer.toString(Integer.parseInt(fullDate.substring(8,10)));
		hour = fullDate.substring(11,13);
		minute = fullDate.substring(14,16);
		setAmPm();
		fixHour();
		
	}
    
	private void fixHour() {
		if(hour.startsWith("0")){
			hour = hour.substring(1);
		}
	}
    
	private void setAmPm(){
		int hourVal = Integer.parseInt(hour);
		if(hourVal<=12){
			amPm = "AM";
			if(hourVal==0){
				hour = "12";
			}
		}else{
			amPm = "PM";
			hour = Integer.toString(hourVal-12);
		}
	}
	
	private void initMonthMap(){
		monthName.put("01", "Jan");
		monthName.put("02", "Feb");
		monthName.put("03", "Mar");
		monthName.put("04", "Apr");
		monthName.put("05", "May");
		monthName.put("06", "Jun");
		monthName.put("07", "Jul");
		monthName.put("08", "Aug");
		monthName.put("09", "Sep");
		monthName.put("10", "Oct");
		monthName.put("11", "Nov");
		monthName.put("12", "Dec");
	}
	
	public String getDate(){
		return (month + " " + day + ", " + hour + ":" + minute + " " + amPm);
	}
    
}
